﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Models;
namespace Web
{
    public partial class admin_workspic : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int wid = InitFromUrl("wid", 0);
            if (!IsPostBack)
            {
                BindData();
            }
            if (wid != 0)
            {
                BindWorksPicInfo(wid);
                BindWorksInfo(wid);
            }
        }
        private int InitFromUrl(string url, int first)
        {
            int rtn = first;
            try
            {
                rtn = Int32.Parse(Request.QueryString[url].ToString());
            }
            catch { }
            return rtn;
        }
        private void BindData()
        {
            BindWorksCategory();
            BindNotice();
            BindTop();
        }
        private void BindWorksCategory()
        {
            List<WorksCategoryInfo> list = WorksCategoryInfoLogic.GetAllWorksCategoryInfo();
            ddlCategory.DataSource = list;
            ddlCategory.DataTextField = "CategoryName";
            ddlCategory.DataValueField = "CategoryID";
            ddlCategory.DataBind();
        }
        private void BindWorksPicInfo(int wid)
        {
            List<WorksPicInfo> list = WorksPicInfoLogic.GetWorkAllPicInfo(wid);
            WorksPicRpt.DataSource = list;
            WorksPicRpt.DataBind();
        }
        private void BindWorksInfo(int wid)
        {
            WorksInfo work = WorksInfoLogic.GetWorksInfo(wid);
            txbTitle.Text = work.Title;
            txbCustomerName.Text = work.CustomerName;
            txbContent.Text = work.WorkContent;
            txbTeamWorkTime.Text = work.TeamWorkTime;
            ddlCategory.SelectedValue = work.CategoryID.ToString();
        }
        private void BindTop()
        {
            List<int> list = new List<int>();
            for (int i = 0; i <= 100; i++)
            {
                list.Add(i);
            }
            ddlTop.DataSource = list;
            ddlTop.DataBind();
        }
        private void BindNotice()
        {
            noticefail.Visible = false;
            noticesuccess.Visible = false;
        }
        protected void btnSubmit_OnClick(object sender, EventArgs e)
        {
            WorksInfo work = createWorksInfo();
            List<WorksPicInfo> piclist = createWorksPicList();
            int isSuccess = WorksInfoLogic.AddWorksInfo(work);
            if (isSuccess == 1)
            {
                int worksid = WorksInfoLogic.GetMaxID();
                foreach (var item in piclist)
                {
                    item.WorksID = worksid;
                    isSuccess += WorksPicInfoLogic.AddWorksPicInfo(item);
                }
                if (isSuccess == piclist.Count() + 1) {
                    noticesuccess.Visible = true;
                }
            }
            else { 

            }

        }
        private WorksInfo createWorksInfo()
        {
            WorksInfo work = new WorksInfo();
            work.CategoryID = Int32.Parse(ddlCategory.SelectedValue);
            work.CustomerName = txbCustomerName.Text.Replace("'","''");
            work.IsNWorks = 1;
            work.HotFlag = 0;
            work.PublishTime = DateTime.Now;
            work.TeamWorkTime = txbTeamWorkTime.Text.Replace("'", "''");
            work.Title = txbTitle.Text;
            work.TopFlag = Int32.Parse(ddlTop.SelectedValue);
            work.WorkContent = txbContent.Text;
            work.ClickCount = 0;
            return work;
        }
        private List<WorksPicInfo> createWorksPicList()
        {
            List<WorksPicInfo> list = new List<WorksPicInfo>();
            if (Request.Form["picurl[]"] != null && Request.Form["piclevel[]"] != null)
            {
                string[] picurls = Request.Form["picurl[]"].Split(',');
                string[] piclevels = Request.Form["piclevel[]"].Split(',');
                string[] picnames = Request.Form["picname[]"].Split(',');
                string top = Request.Form["headshow"];
                for (int i = 0; i < picurls.Count(); i++)
                {
                    WorksPicInfo wpi = new WorksPicInfo();
                    wpi.PicURL = picurls[i];
                    wpi.PicLevel = Int32.Parse(piclevels[i]);
                    wpi.PicName = picnames[i];
                    if (wpi.PicURL == top)
                    {
                        wpi.TopFlag = 1;
                    }
                    else {
                        wpi.TopFlag = 0;
                    }
                    list.Add(wpi);
                }
            }
            return list;
        }
    }
}
